<script setup lang="ts">
import {ref, onMounted, onBeforeUnmount} from 'vue';
import imageURL from '@/assets/background/bgEarth.webp'

interface Star {
  x: number;
  y: number;
  radius: number;
  opacity: number;
  color: string;
  vx: number;
  vy: number;
}

const starryCanvas = ref<HTMLCanvasElement | null>(null);
const numStars = 240;
const stars: Star[] = [];
const canvasWidth = ref(window.innerWidth);
const canvasHeight = ref(window.innerHeight);
const opacity = ref(1);

const initStars = () => {
  if (!starryCanvas.value) return;
  const canvas = starryCanvas.value;
  canvas.width = canvasWidth.value;
  canvas.height = canvasHeight.value;

  const ctx = canvas.getContext('2d');
  if (!ctx) return;

  stars.length = 0;

  for (let i = 0; i < numStars; i++) {
    stars.push({
      x: Math.random() * canvas.width,
      y: Math.random() * canvas.height,
      radius: Math.random() * 1.5 + 0.5,
      opacity: Math.random(),
      color: getRandomColor(),
      vx: 0.5, // 向右移动
      vy: -0.5, // 向上移动
    });
  }
};

const getRandomColor = (): string => {
  const r = Math.floor(Math.random() * 256);
  let g = Math.floor(Math.random() * 256);
  let b = Math.floor(Math.random() * 256);

  // 排除绿色和蓝色
  while ((g > 100 && r < 100) || (b > 100 && r < 100)) {
    g = Math.floor(Math.random() * 256);
    b = Math.floor(Math.random() * 256);
  }

  return `rgba(${r}, ${g}, ${b}, 1)`;
};

const startDrawing = () => {
  if (!starryCanvas.value) return;
  const canvas = starryCanvas.value;
  const ctx = canvas.getContext('2d');
  if (!ctx) return;

  ctx.clearRect(0, 0, canvas.width, canvas.height);

  stars.forEach((star) => {
    star.x += star.vx;
    star.y += star.vy;

    // 如果星星超出右边界，则重置其位置
    if (star.x > canvas.width) {
      star.x = 0;
    }
    // 如果星星超出顶部边界，则重置其位置
    if (star.y < 0) {
      star.y = canvas.height;
    }

    star.opacity -= 0.005;
    if (star.opacity <= 0) {
      star.opacity = 1;
      star.color = getRandomColor();
    }

    ctx.beginPath();
    ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 1.5);
    ctx.fillStyle = star.color;
    ctx.fill();
  });

  requestAnimationFrame(startDrawing);
};

const handleResize = () => {
  canvasWidth.value = window.innerWidth;
  canvasHeight.value = window.innerHeight;
  initStars();
};

const handleScroll = () => {
  const scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
  const scrollRatio = scrollTop / (document.documentElement.scrollHeight - window.innerHeight); // 滚动距离与 100vh 的比率
  // 计算透明度
  opacity.value = Math.max(1 - scrollRatio, 0);
};

onMounted(() => {
  initStars();
  startDrawing();
  window.addEventListener('resize', handleResize);
  window.addEventListener('scroll', handleScroll);
});


onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize);
  window.removeEventListener('scroll', handleScroll);
});
</script>

<template>
  <div class="starry-sky-box w-full h-full">
    <div class="starry-sky-body fixed w-full h-full">
      <canvas class="w-full h-full" ref="starryCanvas"></canvas>
      <img class="starry-sky-image absolute w-full h-full left-0 top-0 right-0 bottom-0" :src="imageURL"
           alt="Earch Background">
    </div>
  </div>
</template>

<style scoped lang="less">
.starry-sky-box {
  pointer-events: none;
  inset: 0;
  display: block;
  opacity: 1;
}

.starry-sky-body {
  inset-block-end: 0;
  inset-inline: 0 0;
  background: linear-gradient(to bottom, #000000, #211d2b, #2c3464);
}

.starry-sky-body canvas {
  position: sticky;
  inset-block-start: 0;
}

.starry-sky-image {
  object-fit: contain;
  object-position: bottom;
  color: transparent;
}
</style>
